package com.cat.twoPointerTechnique;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/sort-colors/
 * @create 2025/8/2 14:31
 * @since JDK17
 */

public class Solution06 {
    public void sortColors(int[] nums) {
        int[] h = new int[3];
        for (int num : nums) h[num]++;
        for (int i = 0, cnt = 0; i < 3; i++) {
            for (int j = 0; j < h[i]; j++) {
                nums[cnt++] = i;
            }
        }
    }

    void swap(int[] nums, int i, int j) {
        nums[i] ^= nums[j];
        nums[j] ^= nums[i];
        nums[i] ^= nums[j];
    }
    public void sortColors02(int[] nums) {
        for (int r = 0, w = 0, b = nums.length - 1; w < b; ) {
            if (nums[r] == 0) { // 红
                r++;
            } else if (nums[r] == 1) {  //
                swap(nums, r, w++);
            } else {
                swap(nums, r, b--);
            }
        }
    }
}
